﻿#include "DragonWarrior.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    DragonWarrior::DragonWarrior(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void DragonWarrior::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        if (getEnvir()->Random->Next(5) > 0)
        {
            if (getEnvir()->Random->Next(3) > 0)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                Broadcast(tempVar);
                Attack1(); //Normal Attack

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 1;
                Broadcast(tempVar2);
                Attack2(); //Spin Attack

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            S::ObjectAttack *tempVar3 = new S::ObjectAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            tempVar3->Type = 2;
            Broadcast(tempVar3);
            Attack3(); //Shield Bash Attack

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }

        if (getTarget()->Dead)
        {
            FindTarget();
        }
    }

    void DragonWarrior::Attack1()
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }
        getTarget()->Attacked(this, damage, DefenceType::ACAgility);
    }

    void DragonWarrior::Attack2()
    {
        std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
                return;
            }
            getTarget()->Attacked(this, damage, DefenceType::ACAgility);
        }
    }

    void DragonWarrior::Attack3()
    {
        PushAttack(1);

        if (getEnvir()->Random->Next(Settings::PoisonResistWeight) >= getTarget()->Stats[Stat::PoisonResist])
        {
            if (getEnvir()->Random->Next(5) == 0)
            {
                Poison *tempVar = new Poison(), *this;
                tempVar->Owner = this;
                tempVar->Duration = 5;
                tempVar->PType = PoisonType::Paralysis;
                tempVar->Value = GetAttackPower(Stats[Stat::MinSC], Stats[Stat::MaxSC]);
                tempVar->TickSpeed = 2000;
                getTarget()->ApplyPoison(tempVar, this);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
        }
    }

    void DragonWarrior::PushAttack(int distance)
    {
        //need to put Damage Stats (DC/MC/SC) on mob for it to push
        int levelGap = 5;
        int mobLevel = this->getLevel();
        int targetLevel = getTarget()->getLevel();

        if ((targetLevel <= mobLevel + levelGap))
        {
            if (getTarget()->Pushed(this, Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()), 2) > 0)
            {
                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC] * 2);
                if (damage == 0)
                {
                    return;
                }
                getTarget()->Attacked(this, damage, DefenceType::ACAgility);
            }
            else
            {
                Attack1(); //Normal Attack
            }
        }
    }
}
